package com.yunxi.service.dubboService.impl.sto;

import com.alibaba.cloud.context.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.yunxi.model.sto.FinanceCompany;
import com.yunxi.service.localService.sto.FinanceCompanyService;
import com.yunxi.sto.pojo.FinanceCompanyListVO;
import com.yunxi.sto.pojo.FinanceCompanyVO;
import com.yunxi.sto.service.FinanceCompanyDubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import java.util.List;

/**
 * <p>
 * 融资机构 服务实现类
 * </p>
 *
 * @author caoYong
 * @since 2019-09-11
 */
public class FinanceCompanyDubboServiceImpl implements FinanceCompanyDubboService {

    @Autowired
    @Qualifier(value = "financeCompanyServiceImpl")
    FinanceCompanyService financeCompanyService;


    @Override
    public FinanceCompanyVO getById(String id) throws Exception {
        FinanceCompany financeCompany = financeCompanyService.getById(id);
        if(financeCompany == null)throw new Exception("操作失败,无法根据主键:"+id+"找到对应记录!");
        FinanceCompanyVO financeCompanyVO = new FinanceCompanyVO();
        BeanUtils.copyProperties(financeCompany, financeCompanyVO);
        return financeCompanyVO;
    }

    @Override
    public void saveOrUpdate(FinanceCompanyVO financeCompanyVO) throws Exception {
        FinanceCompany financeCompany = new FinanceCompany();
        BeanUtils.copyProperties(financeCompanyVO, financeCompany);
        QueryWrapper<FinanceCompany> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("COMPANY_NAME",financeCompany.getCompanyName());
        if(StringUtils.isNotEmpty(financeCompany.getCompanyId())){
            queryWrapper.ne("COMPANY_ID",financeCompany.getCompanyId());
        }
        List<FinanceCompany> list = financeCompanyService.list(queryWrapper);
        if(list != null && list.size() > 0){
            throw new Exception("操作失败,融资机构名称:"+financeCompany.getCompanyName()+"已存在!");
        }
        financeCompanyService.saveOrUpdate(financeCompany);
    }

    @Override
    public void removeById(String id) {
        financeCompanyService.removeById(id);
    }

    @Override
    public PageInfo<FinanceCompanyVO> findPageByVO(FinanceCompanyVO financeCompanyVO, Integer pageNum, Integer pageSize) throws Exception {
        PageInfo<FinanceCompanyVO> pageInfo = financeCompanyService.pageByVo(financeCompanyVO, pageNum, pageSize);
        return pageInfo;
    }

    @Override
    public void removeByListVo(List<FinanceCompanyVO> financeCompanyVOList) throws Exception {
        financeCompanyService.removeByListVo(financeCompanyVOList);
    }

    @Override
    public List listByVo(FinanceCompanyVO financeCompanyVO) {
        return financeCompanyService.listByVo(financeCompanyVO);
    }

    /**
     * 删除恢复
     * @param list 集合
     * @throws Exception 异常
     */
    @Override
    public void delAndReset(List<String> list,Integer isDel) throws Exception {
        if(list == null || list.size() <= 0)throw new Exception("操作失败,list为空!");
        for(String id : list){
            FinanceCompany financeCompany = financeCompanyService.getById(id);
            if(financeCompany == null )throw new Exception("操作失败,无法根据主键:"+id+"找到对应记录!");
            financeCompany.setIsDel(isDel);
            financeCompanyService.saveOrUpdate(financeCompany);
        }
    }

    @Override
    public List<FinanceCompanyListVO> financeList() {
        return financeCompanyService.financeList();
    }
}
